#!/bin/bash

# 检查root权限
if [ "$(id -u)" -ne 0 ]; then
  echo "此脚本需要root权限，请使用sudo执行"
  exit 1
fi

# 设置固定WiFi信息
SSID="wifiname"
PASSWORD="wifiPassword"
 
# 创建自动连接脚本
cat > /usr/local/bin/auto_wifi.sh <<EOF
#!/bin/bash
nmcli dev wifi connect "$SSID" password "$PASSWORD" ifname wlan0
EOF

# 设置执行权限
chmod +x /usr/local/bin/auto_wifi.sh

# 创建systemd服务文件
cat > /etc/systemd/system/auto_wifi.service <<EOF
[Unit]
Description=Auto Connect WiFi
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/auto_wifi.sh
Restart=on-failure

[Install]
WantedBy=multi-user.target
EOF

# 启用并启动服务
systemctl daemon-reload
systemctl enable auto_wifi
systemctl start auto_wifi

# 显示配置结果
echo "配置完成！服务状态："
systemctl status auto_wifi

echo "建议重启系统验证自动连接功能"
